﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace Speedy
{
    public partial class frmCariPromosyon : XtraForm
    {
        public frmCariPromosyon()
        {
            InitializeComponent();
        }

        private void buttonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmListe lst = new frmListe();
            lst.ListeTipi = ListeTipleri.CariKodu;
            lst.Filtre = txtKod.Text;
            if (lst.ShowDialog() == DialogResult.OK)
                if (lst.Value.Length > 0)
                {
                    lblID.Text = lst.Value[0];
                    txtKod.Text = lst.Value[1];
                    txtAd.Text = lst.Value[2];
                }
        }

        private void txtAd_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmListe lst = new frmListe();
            lst.ListeTipi = ListeTipleri.CariAdi;
            lst.Filtre = txtAd.Text;
            if (lst.ShowDialog() == DialogResult.OK)
                if (lst.Value.Length > 0)
                {
                    lblID.Text = lst.Value[0];
                    txtKod.Text = lst.Value[1];
                    txtAd.Text = lst.Value[2];
                }
        }

        private void frmCariPromosyon_Load(object sender, EventArgs e)
        {
            Liste();

            dbPromotionHeader prm = new dbPromotionHeader();
            prm.code.Alias = "CODE";
            prm.name.Alias = "NAME";
            DataTable dt = prm.AllData();
            cmbPromo.ValueMember = "CODE";
            cmbPromo.DisplayMember = "NAME";
            cmbPromo.DataSource = dt;
        }

        private void Liste()
        {
            dbCustomerPromotion cprm = new dbCustomerPromotion();
            gridControl1.DataSource = cprm.AllData();
            gridView1.Columns["cari_RECno"].Visible = false;
            gridView1.Columns["PROMOTION_ID"].Visible = false;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            dbCustomerPromotion prm = new dbCustomerPromotion();
            prm.customerId.SetValue(lblID.Text);
            prm.promoId.SetValue(cmbPromo.SelectedValue);
            prm.Save();
            Liste();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            dbCustomerPromotion prm = new dbCustomerPromotion();
            prm.customerId.SetValue(lblID.Text);
            prm.promoId.SetValue(cmbPromo.SelectedValue);
            prm.Remove();
            Liste();
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gridView1.RowCount > 0 && gridView1.Columns.Count > 0)
                SelectRecord(e.FocusedRowHandle);
        }

        void SelectRecord(int row)
        {
            txtKod.Text = gridView1.GetRowCellValue(row, gridView1.Columns["Cari Kodu"]).ToString();
            txtAd.Text = gridView1.GetRowCellValue(row, gridView1.Columns["Cari Ünvanı"]).ToString();
            cmbPromo.SelectedValue = gridView1.GetRowCellValue(row, gridView1.Columns["PROMOTION_ID"]);
            lblID.Text = gridView1.GetRowCellValue(row, gridView1.Columns["cari_RECno"]).ToString();
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {
            if (gridView1.RowCount > 0 && gridView1.Columns.Count > 0)
                SelectRecord(gridView1.FocusedRowHandle);
        }


    }
}
